﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1.Templates
{
    public class EntityTemplate : TemplatesBase
    {
        public EntityTemplate() : base("Entity")
        {
        }

        public override void SaveFile(List<EntityAttrList> list)
        {
            if (list != null && list.Count > 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                //表描述
                string txtTableDesc = list.FirstOrDefault().TableDescription;
                string txtTableName = list.FirstOrDefault().TableName;
                stringBuilder.AppendLine("using System;");
                stringBuilder.AppendLine("using System.Runtime.Serialization;");
                stringBuilder.AppendLine("using System.ComponentModel.DataAnnotations;");
                stringBuilder.AppendLine("");
                stringBuilder.AppendLine("namespace " + namespacestr);
                stringBuilder.AppendLine("{");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// <summary>");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// " + txtTableDesc);
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// <summary>");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("[Serializable]");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("[DataContract]");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("public partial class " + txtTableName + FileExt);
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("{");


                foreach (var item in list)
                {
                    stringBuilder.Append(new string(' ', 8));
                    stringBuilder.AppendLine("/// <summary>");
                    stringBuilder.Append(new string(' ', 8));
                    stringBuilder.AppendLine("/// " + item.Description);
                    stringBuilder.Append(new string(' ', 8));
                    stringBuilder.AppendLine("/// <summary>");
                    if (item.主键 == 1)
                    {
                        stringBuilder.Append(new string(' ', 8));
                        stringBuilder.AppendLine("[Key]");
                    }
                    stringBuilder.Append(new string(' ', 8));
                    stringBuilder.AppendLine("[DataMember]");
                    stringBuilder.Append(new string(' ', 8));
                    stringBuilder.AppendLine("public " + item.TypeName + " " + item.ColName + " { get; set; }");
                    stringBuilder.AppendLine("");
                }

                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("}");
                stringBuilder.AppendLine("}");

                CreateFile(txtTableName, stringBuilder.ToString());


            }
        }
    }
}
